package org.mifos.boss.testwebservice;

import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.ClientInfoStatus;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.log4j.Logger;
import org.apache.tiles.preparer.PreparerException;
import org.joda.time.DateTime;
import org.mifos.boss.webservice.RESTclient.RESTClient;
import org.mifos.boss.webservice.model.CenterInformationDto;
import org.mifos.boss.webservice.model.ClientInformationDto;
import org.mifos.boss.webservice.model.PersonnelInformationDto;
import org.mifos.boss.webservice.model.SystemInformationDto;
import org.springframework.http.MediaType;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.client.CommonsClientHttpRequestFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class RESTClientControllerOld {

	protected static Logger logger = Logger.getLogger(RESTClientControllerOld.class);
	protected  String URL = "http://localhost:8080/mifos/";
	
	Gson gson;
	RestTemplate restTemplate;
//	public static void main(String[] args) {
//
//		RESTClientController rc = new RESTClientController();
//		String url = "http://localhost:9081/mifos/personnel/id-current.json";
////		
////		HttpHeaders headers = new HttpHeaders();
////		headers.setContentType(MediaType.APPLICATION_JSON);
//		
//		PersonnelInformationDto pt = gson.fromJson(result, PersonnelInformationDto.class);
//
//		logger.debug(pt.getAge() + "======" + pt.getPersonnelDetails().getAddress().getCity());
//		System.out.println(result);
//
//	}
	
	public RESTClientControllerOld(String username, String password){
		restTemplate = prepareRESTTemplate(username, password);
		gson = getGson();
	}
	
	public SystemInformationDto callRESTAdminService(){
		String url = URL + "admin/system/id-information.json";
		String result = invokeRestService(url);
		
		SystemInformationDto sysDto = gson.fromJson(result, SystemInformationDto.class);
		
		return sysDto;
	}
	
	public CenterInformationDto callRESTCenterService(String globalCustNum){
//		center/num-{globalCustNum}
		String url = URL + "center/num-" + globalCustNum +".json";
		String result = invokeRestService(url);
		
		CenterInformationDto centerDto = gson.fromJson(result, CenterInformationDto.class);
		
		return centerDto;
	}
	
	
	public ClientInformationDto callRESTClientService(String globalCustNum){
		//client/num-{globalCustNum}
		
		String url = URL + "client/num-" + globalCustNum +".json";
		String result = invokeRestService(url);
		
		ClientInformationDto clientDto = gson.fromJson(result, ClientInformationDto.class);
		
		return clientDto;
		
	}
	
	
	
	public Gson getGson(){

		gson = (new GsonBuilder()).registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()).create();
		return gson;
	}
	
	public String invokeRestService(String url){
		
		String result = restTemplate.getForObject(url, String.class);
	
		return result;
	}
	
	public RestTemplate prepareRESTTemplate(String username, String password){
		
		RESTClient rs = new RESTClient(username, password);
		HttpClient httpClient = null;
		try {
			httpClient = rs.prepareConnection();
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		CommonsClientHttpRequestFactory cf = new CommonsClientHttpRequestFactory(httpClient);
		 
		
		return new RestTemplate(cf);
	}
	
	
}

class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {
	  // No need for an InstanceCreator since DateTime provides a no-args constructor
	  public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
	    return new JsonPrimitive(src.toString());
	  }
	  public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
	    return new DateTime(json.getAsLong());
	  }
	}

